home *** CD-ROM | disk | FTP | other *** search
/ C# & Game Programming - A…er's Guide (2nd Edition) / Buono 2nd Ed.iso / GameClasses / Wall.cs < prev   
Encoding:
Text File  |  2004-09-07  |  1.9 KB  |  59 lines

  1. // Wall.cs -- A class representing an impassable wall in the game.
  2. using System;
  3. using System.Drawing;
  4.  
  5. namespace GameClasses {
  6.     public class Wall {
  7.         public const int HORIZONTAL = 1;
  8.         public const int VERTICAL = 2;
  9.         public const int DEF_THICKNESS = 8;
  10.  
  11.         public int orientation;
  12.         public Point start;
  13.         public int length;
  14.         public int thickness;
  15.  
  16.         private Brush brush;
  17.         private Color color;
  18.         private Rectangle rect;
  19.  
  20.         public Wall(int orientation, Point start, int length, Color color) :
  21.             this(orientation, start, length, DEF_THICKNESS, color) {}
  22.  
  23.         public Wall(int orientation, Point start, int length, 
  24.             int thickness, Color color) {
  25.             this.orientation = orientation;
  26.             this.start = start;
  27.             this.length = length;
  28.             this.thickness = thickness;
  29.             this.color = color;
  30.       
  31.  
  32.             rect = new Rectangle(start, (orientation == HORIZONTAL) ? 
  33.                 new Size(length, thickness) : new Size(thickness, length));
  34.             this.brush = new SolidBrush(this.color);
  35.         }
  36.  
  37.         public bool Intersects(AnimatedImage obj) {
  38.             int x = obj.imagePosX, y = obj.imagePosY;
  39.             int x2 = obj.imagePosX + obj.imageWidth;
  40.             int y2 = obj.imagePosY + obj.imageHeight;
  41.             int _x = rect.X, _y = rect.Y;
  42.             int _x2 = rect.X + rect.Width, _y2 = rect.Y + rect.Height;
  43.  
  44.             return ((x >= _x && x <= _x2) || (x2 >= _x && x2 <= _x2) ||
  45.                 (_x >= x && _x <= x2) || (_x2 >= x && _x2 <= x2)) &&
  46.                 ((y >= _y && y <= _y2) || (y2 >= _y && y2 <= _y2) ||
  47.                 (_y >= y && _y <= y2) || (_y2 >= y && _y2 <= y2));
  48.         }
  49.  
  50.         public void Display(Graphics g) {
  51.             g.FillRectangle(brush, rect);
  52.         }
  53.  
  54.         public Rectangle getRectangle() { 
  55.             return(rect);
  56.         }
  57.     }
  58. }
  59.